<?php

/**
 * This is the model class for table "{{customer}}".
 *
 * The followings are the available columns in table '{{customer}}':
 * @property integer $customer_id
 * @property string $salutation
 * @property string $first_name
 * @property string $last_name
 * @property string $tel
 * @property string $mobile
 * @property string $tel_company
 * @property string $email_private
 * @property string $email_company
 * @property string $homepage
 * @property string $fax_private
 * @property string $fax_company
 * @property string $birthday
 * @property string $sector
 * @property string $company
 * @property string $profession
 * @property string $registry_date
 * @property string $info
 *
 * The followings are the available model relations:
 * @property Address[] $addresses
 * @property Sale[] $sales
 */
class Customer extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Customer the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{customer}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('salutation', 'length', 'max'=>45),
			array('first_name, last_name, tel, mobile, tel_company, email_private, email_company, homepage, fax_private, fax_company, birthday, sector, company, profession', 'length', 'max'=>100),
			array('registry_date, info', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('customer_id, salutation, first_name, last_name, tel, mobile, tel_company, email_private, email_company, homepage, fax_private, fax_company, birthday, sector, company, profession, registry_date, info', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'addresses' => array(self::HAS_MANY, 'Address', 'customer_id'),
			'sales' => array(self::HAS_MANY, 'Sale', 'customer_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'customer_id' => 'Kundennummer',
			'salutation' => 'Anrede',
			'first_name' => 'Vorname',
			'last_name' => 'Nachname',
			'tel' => 'Telefon Privat',
			'mobile' => 'Mobil',
			'tel_company' => 'Telefon Geschäftlich',
			'email_private' => 'E-Mail Privat',
			'email_company' => 'E-Mail Geschäftlich',
			'homepage' => 'Homepage',
			'fax_private' => 'Fax Privat',
			'fax_company' => 'Fax Geschäftlich',
			'birthday' => 'Geburtstag',
			'sector' => 'Branche',
			'company' => 'Firma',
			'profession' => 'Beruf',
			'registry_date' => 'Registrierungsdatum',
			'info' => 'Info',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('customer_id',$this->customer_id);
		$criteria->compare('salutation',$this->salutation,true);
		$criteria->compare('first_name',$this->first_name,true);
		$criteria->compare('last_name',$this->last_name,true);
		$criteria->compare('tel',$this->tel,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('tel_company',$this->tel_company,true);
		$criteria->compare('email_private',$this->email_private,true);
		$criteria->compare('email_company',$this->email_company,true);
		$criteria->compare('homepage',$this->homepage,true);
		$criteria->compare('fax_private',$this->fax_private,true);
		$criteria->compare('fax_company',$this->fax_company,true);
		$criteria->compare('birthday',$this->birthday,true);
		$criteria->compare('sector',$this->sector,true);
		$criteria->compare('company',$this->company,true);
		$criteria->compare('profession',$this->profession,true);
		$criteria->compare('registry_date',$this->registry_date,true);
		$criteria->compare('info',$this->info,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
            'pagination' => array(    // pagination property
                'pageSize' => 25, // page size
            ),
		));
	}
	
    protected function afterFind ()
    {
        // convert to display format
        $this->registry_date = strtotime($this->registry_date);
        $this->registry_date = date ('d.m.Y', $this->registry_date);

        parent::afterFind ();
    }

    protected function beforeValidate ()
    {
            // convert to storage format
        $this->registry_date = strtotime ($this->registry_date);
        $this->registry_date = date ('Y-m-d', $this->registry_date);

        return parent::beforeValidate ();
    }	

}